package com.jing.system.tags.loadresource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 加载Css资源的Tag
 * @author jing.yue
 * @version 1.0
 * @since 2012-10-12
 */
public class CssTag extends TagSupport {

	private static final long serialVersionUID = -4720851118041402523L;

	/**
	 * 保存csslib的Map集合<br>
	 * key:		路径的别名<br>
	 * value:	路径名称<br>
	 */
	public static Map<String, String> cssMap = new HashMap<String, String>();

	private String csslibs;

	private String webroot;

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			List<String> csslibList = LoadResource.getLibsArr(this.csslibs);
			if(csslibList == null || csslibList.size() == 0) {
				return SKIP_BODY;
			}
			StringBuffer csslibStr= new StringBuffer();
			for (String csslib : csslibList) {
				csslibStr.append("<link href=\"" + webroot + cssMap.get(csslib) + "\" rel=\"stylesheet\" type=\"text/css\" />");
			}
			out.print(csslibStr);
		} catch(Exception e) {
			throw new JspException(e.getMessage());
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * 生命周期结束时调用
	 */
	@Override
	public void release() {
		super.release();
		csslibs = null;
	}

	public String getCsslibs() {
		return csslibs;
	}

	public void setCsslibs(String csslibs) {
		this.csslibs = csslibs;
	}

	public String getWebroot() {
		return webroot;
	}

	public void setWebroot(String webroot) {
		this.webroot = webroot;
	}
}